package chen.web.controller.forum;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import chen.web.user.Account;
import chen.web.user.AccountManager;
import chen.web.user.LostPossswordService;


public class ResetPasswordController {

	private @Autowired AccountManager am;
	private @Autowired LostPossswordService lps;
	
	//重设密码
	@RequestMapping(value="/account/password/reset", method=RequestMethod.PUT)
	public @ResponseBody JSONResponse reset(@RequestParam String code, @RequestParam String password){
		try{
			lps.resetPassword(code, password);	
		}catch(Exception ex){
			
		}
		return null;
	}
	
	//重设密码页面
	@RequestMapping(value="/account/password/reset", method=RequestMethod.GET)
	public String resetPage(@RequestParam(required=false) String code, Model model){
		if(code != null){
			model.addAttribute("code",code);
		}
		return "password-reset";
	}
	
	//请求重设密码
	@RequestMapping(value="/account/password/reset", method=RequestMethod.POST)
	public @ResponseBody JSONResponse sendResetCode(@RequestParam String nickname, @RequestParam String email){
		Account acc = am.getAccountByName(nickname);
		if(acc != null && acc.getEmail().equalsIgnoreCase(email)){
			lps.sendResetCode(acc);	
			return JSONResponse.successed();
		} else {
			return JSONResponse.errorMessage("用户名邮箱不匹配");
		}
	}
}
